package com.crayfish.materiallearn;

import android.graphics.Outline;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;

/**
 * 作者：crayfish(徐杰)
 * 描述：
 * Created by crayfish on 2017/5/2.
 */

public class ElevationActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView txt_elevation;

    private Button add,sub,view_animator,outline;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_elevation);
        initView();
    }

    private void initView(){
        txt_elevation = (TextView)super.findViewById(R.id.txt_elevation);
        txt_elevation.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                int margin = Math.min(view.getWidth(),view.getHeight())/10;
                outline.setRoundRect(margin,margin,view.getWidth()-margin,view.getHeight()-margin,margin/2);
            }
        });
        txt_elevation.setClipToOutline(true);
        add = (Button)super.findViewById(R.id.add);
        sub = (Button)super.findViewById(R.id.sub);
        view_animator = (Button)super.findViewById(R.id.view_animator);
        outline = (Button)super.findViewById(R.id.outline);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        view_animator.setOnClickListener(this);
        outline.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.add:
                txt_elevation.setElevation(txt_elevation.getElevation()+1);
                break;
            case R.id.sub:
                txt_elevation.setElevation(txt_elevation.getElevation()-1);
                break;
            case R.id.view_animator:
                if(txt_elevation.getTranslationX()<=0) {
                    txt_elevation.animate()
                            .translationX(100)
                            .setInterpolator(new DecelerateInterpolator())
                            .start();
                }else{
                    txt_elevation.animate()
                            .translationX(0)
                            .setInterpolator(new DecelerateInterpolator())
                            .start();
                }
                break;
            case R.id.outline:
                if(txt_elevation.getClipToOutline()){//外框显示和隐藏
                    txt_elevation.setClipToOutline(false);
                }else{
                    txt_elevation.setClipToOutline(true);
                }
                break;
        }
    }
}
